package com.quick.proxy.core.utils;

import lombok.Data;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Description: 加锁工具
 * Author: Administrator
 * Date: 2023/7/10
 *
 * @author 吃素不吃肉
 */

public class LockUtils {
    public static Map<String, VisitorLock> VISITOR_LOCK = new ConcurrentHashMap<>();

    @Data
    public static class VisitorLock {
        private Lock lock = new ReentrantLock();
        private Condition condition = getLock().newCondition();

        public void await(long time) {
            this.lock.lock();
            try {
                this.condition.await(time, TimeUnit.MILLISECONDS);
            } catch (InterruptedException var5) {
                var5.printStackTrace();
            } finally {
                this.lock.unlock();
            }
        }

        public void signal() {
            this.lock.lock();
            try {
                this.condition.signal();
            } finally {
                this.lock.unlock();
            }
        }
    }
}
